package com.controller;

import com.domain.ResourceCategory;
import com.domain.ResponseResult;
import com.service.ResourceCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/ResourceCategory")
public class ResourceCategoryController {
    @Autowired
    private ResourceCategoryService resourceCategoryService;
    /*
     * 查询资源分类信息
     * */
    @RequestMapping("/findAllResourceCategory")
    public ResponseResult findAllResourceCategory(){
        List<ResourceCategory> resourceCategories = resourceCategoryService.findAllResourceCategory();
        return new ResponseResult(true,200,"查找所有资源分类信息",resourceCategories);
    }

    /*
    * 新增或修改资源分类
    * */
    @RequestMapping("/saveOrUpdateResourceCategory")
    public ResponseResult saveOrUpdateResourceCategory(@RequestBody ResourceCategory resourceCategory){
        //根据ID是否存在判断是添加还是更新
        if (resourceCategory.getId() == null){
            //添加
            System.out.println(resourceCategory);
            resourceCategoryService.saveResourceCategory(resourceCategory);
            return new ResponseResult(true,200,"添加资源分类成功",null);
        }else {
            //更新
            resourceCategoryService.updateResourceCategory(resourceCategory);
            return new ResponseResult(true,200,"更改资源分类成功",null);
        }
    }

    /*
    * 删除资源分类
    * */
    @RequestMapping("/deleteResourceCategory")
    public ResponseResult deleteResourceCategory(Integer id){
        resourceCategoryService.deleteResourceCategory(id);
        return new ResponseResult(true,200,"删除资源分类成功",null);
    }

}
